package com.huatai.datacenter.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huatai.datacenter.entity.RequestLogEntity;
import com.huatai.datacenter.entity.ServerObjectEntity;
import com.huatai.datacenter.entity.TbFireDataRequestLogEntity;
import com.huatai.datacenter.mapper.RequestMapper;
import com.huatai.datacenter.mapper.ServerObjectMapper;
import com.huatai.datacenter.mapper.TbFireDataRequestLogEntityMapper;
import com.huatai.datacenter.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lion
 * @date 2023/7/19  10:09
 */
@Service
public class RequestService extends BaseService<RequestMapper, RequestLogEntity>{

	@Autowired
	private TbFireDataRequestLogEntityMapper tbFireDataRequestLogEntityMapper;

	@Autowired
	private ServerObjectMapper ServerObjectMapper;
	public List<Map> dataCount(){

		List<Map> list = new ArrayList<>();
		QueryWrapper<RequestLogEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("requestStatus","success");
		Map<String,Long> successMap = new HashMap<>();
		successMap.put("成功次数",this.count(queryWrapper));
		list.add(successMap);

		Map<String,Long> errorMap = new HashMap<>();
		QueryWrapper<RequestLogEntity> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.eq("requestStatus","error");
		errorMap.put("失败次数",this.count(queryWrapper1));
		list.add(errorMap);

		return list;
	}

	@Autowired
	private RedisTemplate redisTemplate;
	public Long saveLog(String url,String status,String remark,String detail,String requestName,Integer size){

		RequestLogEntity requestLog = new RequestLogEntity();
		requestLog.setRequestURL(url);
		requestLog.setRequestStatus(status);
		requestLog.setDetail(detail);
		requestLog.setRemark(remark);
		requestLog.setRequestName(requestName);
		this.SaveWithValid(requestLog);

		TbFireDataRequestLogEntity FireDatarequestlog = new TbFireDataRequestLogEntity();
		JSONObject jsonObject = JSONObject.parseObject(detail);
		String appkey = jsonObject.getString("appKey");
		ServerObjectEntity ObjectEntity = ServerObjectMapper.getByAppKey(appkey);

		FireDatarequestlog.setRequestName(requestName);
		FireDatarequestlog.setRequestURL(url);
		FireDatarequestlog.setRequestStatus(status);
		FireDatarequestlog.setRemark(remark);
		FireDatarequestlog.setDetail(ObjectEntity.getServerObjectName());
		FireDatarequestlog.setQuestNum(1);
		FireDatarequestlog.setDataNum(size);
		FireDatarequestlog.setCreateTime(LocalDateTime.now());
		FireDatarequestlog.setUpdateTime(LocalDateTime.now());
		FireDatarequestlog.setSpare1(null);
		FireDatarequestlog.setSpare2(null);
		tbFireDataRequestLogEntityMapper.insert(FireDatarequestlog);


		String key = RedisUtil.buildDbTableKey(RequestLogEntity.class);
		List<RequestLogEntity> list = this.list();
		RedisUtil.set(key, list);

		return requestLog.getId();
	}

}
